#include "ChatClent.h"

ChatClent::ChatClent(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("Chat Client");                  // 设置标题
    hostLabel = new QLabel("Server IP:");
    portLabel = new QLabel("Server Port:");
    nameLabel = new QLabel("Name:");
    messageLabel = new QLabel("Message:");
    hostEdit = new QLineEdit("127.0.0.1");          // 默认IP
    portEdit = new QLineEdit("6000");               // 默认端口
    nameEdit = new QLineEdit;
    messageEdit = new QLineEdit;
    connectBtn = new QPushButton("Connect");
    sendBtn = new QPushButton("Send");
    quitBtn = new QPushButton("Quit");
    chatEdit = new QTextEdit;
    connect(connectBtn, SIGNAL(clicked()), this, SLOT(connectToServer()));  // 连接服务器
    connect(sendBtn, SIGNAL(clicked()), this, SLOT(sendMessage()));         // 发送信息
    connect(quitBtn, SIGNAL(clicked()), this, SLOT(close()));               // 关闭

    // 布局：
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *hostLayout = new QHBoxLayout;
    QHBoxLayout *portLayout = new QHBoxLayout;
    QHBoxLayout *nameLayout = new QHBoxLayout;
    QHBoxLayout *messageLayout = new QHBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    hostLayout->addWidget(hostLabel);
    hostLayout->addWidget(hostEdit);
    portLayout->addWidget(portLabel);
    portLayout->addWidget(portEdit);
    nameLayout->addWidget(nameLabel);
    nameLayout->addWidget(nameEdit);
    messageLayout->addWidget(messageLabel);
    messageLayout->addWidget(messageEdit);
    buttonLayout->addWidget(connectBtn);
    buttonLayout->addWidget(sendBtn);
    buttonLayout->addWidget(quitBtn);
    mainLayout->addLayout(hostLayout);
    mainLayout->addLayout(portLayout);
    mainLayout->addLayout(nameLayout);
    mainLayout->addLayout(messageLayout);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addWidget(chatEdit);
    setLayout(mainLayout);

    // 套接字
    socket = new QTcpSocket(this);

    // 连接和断开信号
    connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
}

ChatClent::~ChatClent()
{
}

// 连接服务器
void ChatClent::connectToServer()
{
    QString host = hostEdit->text();
    quint16 port = portEdit->text().toInt();
    qDebug() << "Connecting to server:" << host << port;
    socket->connectToHost(QHostAddress(host), port);
}

// 连接上以后，关闭按钮
void ChatClent::connected()
{
    qDebug() << "Connected to server.";
    sendBtn->setEnabled(true);
}

// 读取信息
void ChatClent::readyRead()
{
    while (socket->canReadLine()) {
        QString message = QString::fromUtf8(socket->readLine()).trimmed();
        qDebug() << "Message from server:" << message;
        chatEdit->append(message);
    }
}

// 断开连接
void ChatClent::disconnected()
{
    qDebug() << "Disconnected from server.";
    sendBtn->setEnabled(false);
}

// 发送信息
void ChatClent::sendMessage()
{
    QString message = nameEdit->text() + ": " + messageEdit->text();
    socket->write(message.toUtf8());
    socket->flush();
    messageEdit->clear();
}

